# coding=utf-8
import wx
from TechDao import TechDao

class TechInputFrame(wx.Frame):
    
    def __init__(self,mainframe,op,id):
        titlemap={"add":u"增加","edit":u"修改","view":u"查看"}
        wx.Frame.__init__(self,None,-1,titlemap[op],style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX),size=(600,400))
        self.mainframe=mainframe
        self.op=op
        self.id=id
        self.Center()
        self.panel=wx.Panel(self)
        self.titlelabel=wx.StaticText(self.panel,-1,label=u"标题",pos=(30,30))
        self.titletext = wx.TextCtrl(self.panel, -1,pos=(60,30),size=(500,20))
        self.contentlabel=wx.StaticText(self.panel,-1,label=u"内容",pos=(30,60))
        self.contenttext = wx.TextCtrl(self.panel, -1,pos=(60,60),size=(500,260),style=wx.TE_MULTILINE|wx.TE_DONTWRAP)
        self.okbutton=wx.Button(self.panel,label=u"确定",pos=(200,330))
        self.closebutton=wx.Button(self.panel,label=u"关闭",pos=(280,330))
        self.Bind(wx.EVT_BUTTON,self.OnOk,self.okbutton)
        self.Bind(wx.EVT_BUTTON,self.OnExit,self.closebutton)
        self.Bind(wx.EVT_CLOSE,self.OnDestroy)
        if self.op=="edit":
            techdao=TechDao()
            row=techdao.select("and id=%s"%(self.id))[0]
            self.titletext.SetValue(row[1])
            self.contenttext.SetValue(row[4])
    
    def OnOk(self,event):
        techdao=TechDao()
        if self.op=="add":
            techdao.insert(self.titletext.GetValue(),self.contenttext.GetValue())            
            self.contenttext.SetValue("")
            self.titletext.SetValue("")
        else:
            techdao.update(self.id,self.titletext.GetValue(),self.contenttext.GetValue())
        wx.MessageBox(u"数据保存成功",u"提示", wx.OK | wx.ICON_INFORMATION, self)
        self.mainframe.LoadData()
    
    def OnExit(self,event):
        self.Close()
    
    def OnDestroy(self,event):
        self.Destroy()